<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改项目分类' : '新增项目分类'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="100px" :rules="rules">
      <el-form-item label="分类名称" prop="internalTypeName">
        <el-input v-model="form.internalTypeName" placeholder="请输入分类名称" />
      </el-form-item>

      <el-form-item label="排序" prop="typeOrder">
        <el-input type="number" v-model.number="form.typeOrder" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { typeCreate, typeUpdate } from '@/api/satisfaction'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      visible: false,
      isEdit: false,
      form: {
        internalTypeName: null,
        typeOrder: 0
      },
      selectData: [],
      rules: {
        internalTypeName: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        typeOrder: [
          { type: 'number', message: '请输入数字', trigger: 'blur' },
          { required: true, message: '请输入排序值', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
      if (!bool) {
        // 新增设置为0
        this.$set(this.form, 'typeOrder', 0)
      }
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await typeUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await typeCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
